<%@ page import="model.Article" %>
<%@ page import="model.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    List<Article> articles = (List<Article>)request.getAttribute("articles");
%>
<!DOCTYPE html>
<html>
<head>
    <title>文章列表 - 回声网络</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <style>
        body { margin: 0; padding: 0; background: #f5f5f5; font-family: Arial; }
        .container { max-width: 1000px; margin: 0 auto; padding: 20px; }
        .header { background: #409EFF; color: white; padding: 0 20px; line-height: 60px; margin-bottom: 20px; }
        .article-list { background: white; padding: 20px; border-radius: 4px; }
        .article-item { padding: 15px; border-bottom: 1px solid #f0f0f0; }
        .article-item:last-child { border-bottom: none; }
        .article-title { font-size: 18px; font-weight: bold; color: #333; margin-bottom: 8px; }
        .article-title a { color: #333; text-decoration: none; }
        .article-title a:hover { color: #409EFF; }
        .article-meta { color: #666; font-size: 14px; }
        .empty-state { text-align: center; padding: 40px; color: #666; }
    </style>
</head>
<body>
<div class="header">
    <span style="font-size: 20px; font-weight: bold;">回声网络</span>
    <div style="float: right;">
        <a href="index.jsp" style="color: white; margin-right: 15px;">首页</a>
        <a href="publish.html" style="color: white; margin-right: 15px;">发布文章</a>
    </div>
</div>

<div class="container">
    <div class="article-list">
        <h2 style="margin-bottom: 20px;">文章列表</h2>

        <% if (articles == null || articles.isEmpty()) { %>
        <div class="empty-state">
            暂无文章，<a href="publish.html" style="color: #409EFF;">去发布第一篇</a>
        </div>
        <% } else { %>
            <% for (Article article : articles) { %>
            <div class="article-item">
                <div class="article-title">
                    <a href="articleDetail?id=<%= article.getId() %>"><%= article.getTitle() %></a>
                    <% 
                        // 检查当前用户是否为文章作者
                        boolean isAuthor = false;
                        User currentUser = (User)session.getAttribute("user");
                        if (currentUser != null && article.getUserId() != null) {
                            isAuthor = article.getUserId().equals(currentUser.getId());
                        }
                    %>
                    <% if (isAuthor) { %>
                    <form action="deleteArticle" method="post" style="display: inline; margin-left: 20px;">
                        <input type="hidden" name="id" value="<%= article.getId() %>">
                        <button type="submit" class="delete-btn" onclick="return confirm('确定要删除这篇文章吗？')">删除</button>
                    </form>
                    <% } %>
                </div>
                <div class="article-meta">
                    作者：<span><%= article.getAuthorName() %></span> |
                    发布时间：<span><%= article.getCreatedAt() %></span> |
                    阅读量：<span><%= article.getViewCount() %></span>
                </div>
            </div>
            <% } %>
        <% } %>
    </div>
</div>
</body>
</html>